/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QPainter>
#include <QTextOption>
#include <QStyleOptionGraphicsItem>

#include "ganttaxis.h"
#include "procycle.h"

const float INTER_COEFS [] = { 2, 2.5, 2 };   // 1, 2, 5, 10, 20, 50 ...

const float MIN_INTER_GRADS = 40;


GanttAxis::GanttAxis(ProCycle* app) :
	PCChild(app),
	m_length(1),
	m_scale(1)
{
}


QRectF GanttAxis::boundingRect() const {
	return QRectF(0, 0, scene()->width(), scene()->height());
}


void GanttAxis::paint(
		QPainter *painter,
		const QStyleOptionGraphicsItem* /*option*/,
		QWidget*)
{
	painter->save();

	QRect r = QRect(0, 0, scene()->width(), m_app->headerHeight());
	painter->fillRect(r, Qt::lightGray);
	QPen liner (Qt::gray);
	liner.setWidth(1);
	painter->setPen(liner);
	painter->drawRect(r);

	//QList<int> grads;
	float u2p = unit2px();
	float interG = 1.f;
	int i=0;
	while (interG*u2p < MIN_INTER_GRADS) {
		interG *= INTER_COEFS[i];
		if (++i == 3) {
			i = 0;
		}
	}
	int gradCount = m_length/interG + 1;
	float diff = m_app->headerHeight() / 6;
	QPen texter (Qt::black);
	texter.setWidth(1);
	for (i=0; i<gradCount; i++) {
		float time = i*interG;
		float px = u2p * time;

		painter->setPen(texter);
		painter->drawText(
				QPoint(px+diff+1, m_app->headerHeight()-diff),
				QString::number(time));

		painter->setPen(liner);
		painter->drawLine(px, 0, px, scene()->height());
	}

	painter->restore();
}





